<?php
/**
 * Created by JetBrains PhpStorm.
 * User: shaoshuai
 * Date: 12-7-16
 * Time: 下午9:16
 * To change this template use File | Settings | File Templates.
 */
class Room
{
    /**
     * @var string
     */
    protected $id;

    /**
     * @var User
     */
    protected $owner;

    /**
     * @var Chessboard
     */
    protected $ownerChessboard;

    /**
     * @var User
     */
    protected $guest;

    /**
     * @var Chessboard
     */
    protected $guestChessboard;

    /**
     * @var string
     */
    protected $file;

    /**
     * 现在该谁fire
     * @var string
     */
    protected $current;

    /**
     * @var bool
     */
    protected $isOver;

    /**
     * @var string
     */
    protected $winner;

    /**
     * @param string $id
     */
    public function __construct($id)
    {
        $this->id = $id;
        $this->file = $id . '.data';
        $this->current = 'owner';
        $this->isOver = false;
    }

    /**
     * @param string $role
     * @return bool
     */
    public function isMyTurn($role)
    {
        return $role == $this->current;
    }

    /**
     * @param string $who
     * @param int $x
     * @param int $y
     * @return int
     */
    public function fire($who, $x, $y)
    {
        if ($this->isOver) {
            return -1;
        }
        $status = 0; //not my turn
        if ($this->isMyTurn($who)) {
            if ($who == 'owner') {
                //fire guest
                $status = $this->guestChessboard->onFire($x, $y);
                if ($this->guestChessboard->isOver()) {
                    //guest lost game
                    $this->isOver = true;
                    $this->winner = 'owner';
                }
                $this->current = 'guest';
            } elseif ($who == 'guest') {
                //fire owner
                $status = $this->ownerChessboard->onFire($x, $y);
                if ($this->ownerChessboard->isOver()) {
                    $this->isOver = true;
                    $this->winner = 'guest';
                }
                $this->current = 'owner';
            }
        }
        return $status;
    }

    /**
     * @return bool
     */
    public function isOver()
    {
        return $this->isOver;
    }

    /**
     * @return string
     */
    public function getWinner()
    {
        return $this->winner;
    }

    public function save()
    {
        file_put_contents($this->file, serialize($this));
    }

    /**
     * @return bool
     */
    public function isReady()
    {
        return isset($this->ownerChessboard) && isset($this->guestChessboard);
    }

    public function noticeFighter()
    {
        EventDispatcher::dispatch($this->owner, new FightStartEvent());
        EventDispatcher::dispatch($this->guest, new FightStartEvent());
    }

    /**
     * @static
     * @param string $id
     * @return Room
     */
    public static function fromId($id)
    {
        return unserialize(file_get_contents($id . '.data'));
    }

    /**
     * create room and return room id
     * @static
     * @return Room
     */
    public static function create()
    {
        $id = uniqid('room_');
        file_put_contents($id . '.data', '{}');
        return new Room($id);
    }

    /**
     * @return User
     */
    public function getGuest()
    {
        return $this->guest;
    }

    /**
     * @return User
     */
    public function getOwner()
    {
        return $this->owner;
    }

    /**
     * @param User $guest
     */
    public function setGuest($guest)
    {
        $this->guest = $guest;
    }

    /**
     * @param User $owner
     */
    public function setOwner($owner)
    {
        $this->owner = $owner;
    }

    /**
     * @param string $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return string
     */
    public function getId()
    {
        return $this->id;
    }

    public static function test($file)
    {
        include 'User.php';
        include 'Chessboard.php';
        include 'Airplane.php';
        print_r(unserialize(file_get_contents($file)));
    }

    /**
     * @param Chessboard $guestChessboard
     */
    public function setGuestChessboard($guestChessboard)
    {
        $this->guestChessboard = $guestChessboard;
    }

    /**
     * @param Chessboard $ownerChessboard
     */
    public function setOwnerChessboard($ownerChessboard)
    {
        $this->ownerChessboard = $ownerChessboard;
    }
}

//Room::test('room_50043190d010a.data');
